﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

using LEEAAttendanceControl.AttendanceReference;

using System.Windows.Browser;

using LEEAAttendanceControl.Web;
using LEEAAttendanceControl.Web.Services;

using System.ServiceModel;
using System.ComponentModel;
using System.Collections.ObjectModel;

using System.ServiceModel.DomainServices.Client;

namespace LEEAAttendanceControl.Views
{
    public partial class SpecificReports : Page
    {
        LEEAContext context = new LEEAContext();

        public SpecificReports()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            App myApp = (App)Application.Current;

            myApp.ClearEvents();

            myApp.ChangeReportTitleEvent += ChangeReportTitleHandler;

            myApp.ReportName = "Summary Time Card Report";

            context.Load(context.GetEmployeeByEmployeeIDQuery(((App)Application.Current).EmployeeId)
                , OnEmployeeLoadCompleted, null);
        }

        private void OnEmployeeLoadCompleted(LoadOperation load)
        {
            Employee employee = load.Entities.FirstOrDefault() as Employee;

            if (employee != null)
            {
                FirstNameBox.Text = employee.FirstName;

                LastNameBox.Text = employee.LastName;
            }
        }

        private void ChangeReportTitleHandler(object sender, EventArgs e)
        {
            App myApp = (App)Application.Current;

            ReportNameBlock.Text = myApp.DisplayName + ": " + myApp.ReportName;

            //--------------------------------------------------------------------
            //--> Clear Duration when switch from Report Type to another Report Type:

            FromPicker.SelectedDate = null;
            ToPicker.SelectedDate = null;
        }

        //--------------------------------------------------------------------
        //--> Generate Report:

        private void GenerateButton_Click(object sender, RoutedEventArgs e)
        {
            if (FromPicker.SelectedDate != null && ToPicker.SelectedDate != null)
            {
                AttendanceServiceClient proxy = General.General.GetProxy();

                proxy.SetReportDataCompleted += OnDataSettingCompleted;

                int employeeID = ((App)Application.Current).EmployeeId;

                int companyID = ((App)Application.Current).CompanyID == null
                    ? 0 : ((App)Application.Current).CompanyID.Value;

                proxy.SetReportDataAsync(new ObservableCollection<int>() { employeeID }, FromPicker.SelectedDate.Value.Date
                    , ToPicker.SelectedDate.Value.Date, companyID);
            }
        }

        private void OnDataSettingCompleted(object sender, AsyncCompletedEventArgs e)
        {
            string report = String.Empty;

            switch (((App)Application.Current).ReportName)
            {
                case "Summary Time Card Report":
                    report = "SummaryTimeCard";

                    break;

                case "Detailed Time Card Report":
                    report = "DetailedTimeCard";

                    break;

                case "Detailed Overtime Report":
                    report = "DetailedOvertime";

                    break;

                case "Shift Assignments Report":
                    report = "ShiftAssignments";

                    break;

                case "Exception Assignments Report":
                    report = "ExceptionAssignments";

                    break;

                case "Late Comers Report":
                    report = "LateComers";

                    break;

                case "Absence Report":
                    report = "Absence";

                    break;

                case "Saved Hours Report":
                    report = "SavedHours";

                    break;

                case "Missing Punches Report":
                    report = "MissingPunches";

                    break;
            }

            string url = General.General.GetUrl() + "/ReportPages/Reports.aspx?Name=" + report;

            HtmlPage.Window.Navigate(new Uri(url), "_newWindow"
                , "resizable=1,scrollbars=1,status=1,top=110,left=75,width=1130,height=606");
        }
    }
}
